def calculateDiaryScore(diaryContent, chooseEmo):
    from snownlp import SnowNLP
    import math

    # 20分基础分，其余为80分
    # 表情得分默认占比因子2，文本的占比因子：日记的句子数以及负面情绪在句子中的占比
    emo_score_map = {'angel': '5', 'angry': '5', 'blink': '25', 'confused': '10', 'cool-1': '20', 'cool': '25', 'corpse': '5', 'cry-1': '10', 'cry': '10', 'devil': '15', 'dizzy': '10', 'expressionless-1': '20', 'expressionless': '15', 'fall-ill': '15', 'haha-1': '20', 'haha': '20', 'happy-1': '15', 'happy-2': '20', 'happy-3': '20', 'injured': '5', 'jealous': '25', 'kiss-1': '25', 'kiss-2': '25', 'kiss': '25', 'laughcry': '25', 'love': '25', 'lue-1': '20', 'lue-2': '25', 'lue': '20', 'mask': '15', 'muddleb': '20', 'mute': '15', 'poisoning-1': '5', 'poisoning': '10', 'sad-1': '5', 'sad-2': '10', 'shocked-1': '15', 'beshocked': '20', 'shut-up': '10', 'sleep': '15', 'sly-smile': '25', 'smile-1': '5', 'squint': '10', 'sweating': '5', 'thinking': '10', 'tired': '10', 'worry-1': '15', 'worry': '15', 'yue-1': '5', 'yue': '5'}

    s = SnowNLP(diaryContent)
    total_sentences_num = len(s.sentences)
    bad = 0.0
    sum_text_score = 0.0
    for sentence in s.sentences:
        if SnowNLP(sentence).sentiments < 0.5:
            bad = bad + 1
        # print(SnowNLP(sentence).sentiments)
        sum_text_score = sum_text_score + min(((SnowNLP(sentence).sentiments * 100 ) + 10), 100)

    x = int(emo_score_map[chooseEmo]) / 25 * 100
    x_rate = 4
    y = sum_text_score / total_sentences_num
    y_rate = min(total_sentences_num, 1 + math.log(total_sentences_num)) + (bad / total_sentences_num) * 10
    total_rate = x_rate + y_rate

    total_score = 20 + 80 * ( x * (x_rate / total_rate) + y * (y_rate / total_rate)) * 0.01

    return int(total_score)

def get_week_date(before_week):
    import datetime
    from datetime import timedelta

    now = datetime.datetime.now()
    this_week_start = now - timedelta(days=now.weekday())
    this_week_end = now + timedelta(days=6 - now.weekday())
    week_start = this_week_start - timedelta(7 * before_week)
    week_end = this_week_end - timedelta(7 * before_week)
    return week_start, week_end

def get_month_date(year_num, month_num):
    import datetime
    from datetime import timedelta

    month_start = datetime.datetime(year_num, month_num, 1)
    if month_start.month == 12:
        month_end = datetime.datetime(year_num+1, 1, 1) - timedelta(days=1)
    else:
        month_end = datetime.datetime(year_num, month_num+1, 1) - timedelta(days=1)
    return month_start, month_end

def get_year_date(year_num):
    import datetime
    from datetime import timedelta

    year_start = datetime.datetime(year_num, 1, 1)
    year_end = datetime.datetime(year_num+1, 1, 1) - timedelta(days=1)
    return year_start, year_end